np.where(三元运算符)
# 判断前4名学生,前4门课程中,成绩大于60的值为1,否则为0
>>> temp = score[:4, :4]
>>> np.where(temp > 60, 1, 0)
array([[1, 1, 1, 0],
[1, 1, 1, 1],
[1, 1, 0, 0],
[1, 0, 0, 0]])
- 复合逻辑需要结合
np.logical_and
和np.logical_or
使用
# 判断前4名学生,前四门课程中,成绩大于60且小于90的值为1,否则为0
>>> np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
array([[1, 1, 0, 0],
[0, 1, 1, 0],
[1, 1, 0, 0],
[1, 0, 0, 0]])
# 判断前4名学生,前四门课程中,成绩大于90或小于60的值为1,否则为0
>>> np.where(np.logical_or(temp > 90, temp < 60), 1, 0)
array([[0, 0, 1, 1],
[1, 0, 0, 1],
[0, 0, 1, 1],
[0, 0, 1, 1]])